package com.ruoyi.app.controller;

import com.ruoyi.app.domain.co.user.CoForgotPassword;
import com.ruoyi.app.domain.co.user.SysUserVo;
import com.ruoyi.common.enums.SysUserIsAdminEnum;
import com.ruoyi.framework.security.RegisterBody;
import com.ruoyi.framework.security.service.SysLoginService;
import com.ruoyi.framework.security.service.SysRegisterService;
import com.ruoyi.framework.sms.SmsClient;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.R;
import com.ruoyi.project.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户信息
 * 
 * @author ruoyi
 */
@Api(value = "手机端用户", tags = "用户管理接口")
@RestController
@RequestMapping("/app/user")
public class AppUserController extends BaseController {

    @Autowired
    private SysRegisterService sysRegisterService;
    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private SmsClient smsClient;

    @Autowired
    private ISysUserService userService;

    /**
     * 注册
     */
    @ApiOperation("注册用户")
    @PostMapping("/register")
    public R<String> register(@RequestBody RegisterBody registerBody)
    {
        registerBody.setIsAdmin(SysUserIsAdminEnum.APP.getValue());
        registerBody.setUsername(registerBody.getMobile());
        String s = sysRegisterService.smsCodeRegister(registerBody);
        return R.ok(s);
    }
    

    /**
     * 登录
     */
    @ApiOperation("手机验证码登录")
    @PostMapping("/smsLogin")
    public R<String> smsLogin(@RequestBody RegisterBody registerBody)
    {
        String s = sysLoginService.smsLogin(registerBody.getMobile(), registerBody.getCode());
        return R.ok(s);
    }

    /**
     * 登录
     */
    @ApiOperation("密码登录")
    @PostMapping("/login")
    public R<String> codeLogin(@RequestBody RegisterBody registerBody)
    {
        String s = sysLoginService.passwordLogin(registerBody.getMobile(), registerBody.getPassword());
        return R.ok(s);
    }

    /**
     * 发送验证码
     */
    @ApiOperation("发送验证码")
    @PostMapping("/sendCode")
    public R<String> sendCode(@RequestBody RegisterBody registerBody)
    {
        smsClient.sendCode(registerBody.getMobile());
        return R.ok("发送成功");
    }


    /**
     * 忘记密码
     */
    @ApiOperation("忘记密码")
    @PostMapping("/forgotPassword")
    public R<String> forgotPassword(@RequestBody CoForgotPassword forgotPassword)
    {
        sysLoginService.forgotPassword(forgotPassword);
        return R.ok("修改成功");
    }

    /**
     * 获取用户信息
     */
    @ApiOperation("获取用户信息")
    @GetMapping("/getUserInfo")
    public R<?> getUserInfo()
    {
        SysUserVo userVo = sysLoginService.getUserInfo(getUserId());
        return R.ok(userVo);
    }

}
